﻿/*----------------------------------------------------------------
// 资源中心
// Copyright (C) 2006-2008 
//
// 文件名:GenerateThumbnail.cs
// 文件功能描述：生成缩略图函数
//
// 创建人:张沛沛
// 创建日期: 2008.08.10
// 
// 修改标识:
// 修改描述:
//
//----------------------------------------------------------------*/

using System;
using System.Collections.Generic;
using System.Text;

using System.Drawing;
using System.Drawing.Imaging;

namespace WebUtility
{
    public class GenerateThumbnail
    {
        /// <summary>
        /// 使用 System.Drawing.Graphics 库重画图像
        /// </summary>
        /// <param name="bmpSource"></param>
        /// <param name="thumWi"></param>
        /// <param name="thumHi"></param>
        /// <param name="maintainAspect"></param>
        /// <returns></returns>
        public static Bitmap CreateThumbnail(Bitmap bmpSource, int thumWi, int thumHi, bool maintainAspect)
        {
            if (bmpSource.Width < thumWi && bmpSource.Height < thumHi)
            {
                return bmpSource;
            }

            Bitmap ret = null;

            try
            {
                int wi, hi;

                wi = thumWi;
                hi = thumHi;

                if (maintainAspect)
                {
                    if (bmpSource.Width > bmpSource.Height)
                    {
                        wi = thumWi;
                        hi = (int)(bmpSource.Height * ((decimal)thumWi / bmpSource.Width));
                    }
                    else
                    {
                        hi = thumHi;
                        wi = (int)(bmpSource.Width * ((decimal)thumHi / bmpSource.Height));
                    }
                }

                ret = new Bitmap(wi, hi);

                using (Graphics g = Graphics.FromImage(ret))
                {
                    g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
                    g.FillRectangle(Brushes.White, 0, 0, wi, hi);
                    g.DrawImage(bmpSource, 0, 0, wi, hi);
                }
            }
            catch
            {
                ret = null;
            }

            return ret;
        }


        /// <summary>
        /// 混入JPEG压缩来真正的优化结果
        /// </summary>
        /// <param name="bmpSource">原始图片</param>
        /// <param name="thumWi">缩略宽度</param>
        /// <param name="thumHi">缩略高度</param>
        /// <param name="maintainAspect"></param>
        /// <param name="saveToFileName">要保存到的文件名</param>
        public static void UseJPEG(string sourceFile, int thumWi, int thumHi, bool maintainAspect, string saveToFileName)
        {

            Bitmap bmpSource = (Bitmap)Bitmap.FromFile(sourceFile);

            Image myThumbnail = CreateThumbnail(bmpSource, thumWi, thumHi, false);

            EncoderParameters encoderParams = new EncoderParameters();
            long[] quality = new long[1];
            quality[0] = 90;

            EncoderParameter encoderParam = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, quality);

            encoderParams.Param[0] = encoderParam;

            ImageCodecInfo[] arrayICI = ImageCodecInfo.GetImageEncoders();

            ImageCodecInfo jpegICI = null;

            for (int x = 0; x < arrayICI.Length; x++)
            {
                if (arrayICI[x].FormatDescription.Equals("JPEG"))
                {
                    jpegICI = arrayICI[x];
                    break;
                }
            }

            myThumbnail.Save(saveToFileName, jpegICI, encoderParams);

        }
    }
}
